package cn.lirenyi.user.controller;


import cn.lirenyi.basic.exception.BusinessException;
import cn.lirenyi.basic.util.AjaxResult;
import cn.lirenyi.user.Dto.LoginDto;
import cn.lirenyi.user.domain.BinderDto;
import cn.lirenyi.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService iLogininfoService;

    @PostMapping("/acount")
    public AjaxResult loginAccount(@RequestBody LoginDto loginDto){
        try {
            //不仅要校验，还有返回值，所以在里面直接封装AjaxResult更好
            Map<String,Object> map = iLogininfoService.loginAcount(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误，请骚后再试");
        }
    }

    /**
     *  微信登录，扫码之后的回调接口
     * @param params
     * @return
     */

    @PostMapping("/wechat")                     //  参数里面只有code有用
    public AjaxResult wechat(@RequestBody Map<String,String> params){
        try {
            //不仅要校验，还有返回值，所以在里面直接封装AjaxResult更好
            return iLogininfoService.wechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误，请骚后再试");
        }
    }





    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody BinderDto binderDto){
        try {
            //不仅要校验，还有返回值，所以在里面直接封装AjaxResult更好
            Map<String, Object> binder = iLogininfoService.binder(binderDto);
            return AjaxResult.me().setResultObj(binder);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统错误，请骚后再试");
        }
    }

}
